//
//  MoreStoryCollectionVC.m
//  YouMin_iOS
//
//  Created by lanou3g on 15/12/10.
//  Copyright © 2015年 wey. All rights reserved.
//

#import "MoreStoryCollectionVC.h"
#import "SecondCollectionViewCell.h"
#import "EntityModel.h"
@interface MoreStoryCollectionVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *itemsArr;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@end

@implementation MoreStoryCollectionVC


static NSString * const reuseIdentifier = @"secondCellMore";
- (NSMutableArray *)itemsArr {
    
    if (!_itemsArr) {
        _itemsArr = [NSMutableArray arrayWithCapacity:12];
    }
    return _itemsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.flowLayout.minimumInteritemSpacing = 2.0;
    self.flowLayout.minimumLineSpacing = 2.0;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(1.0, 3.0, 1.0,3.0);
    self.flowLayout.itemSize = CGSizeMake((kScreenWith -15)/2, [UIScreen mainScreen].bounds.size.height/3.5);
    
    //**  下拉,上拉不加载  */
    kWeakSelf(weakSelf);
    weakSelf.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
        [weakSelf.collectionView.mj_footer  endRefreshing];
    }];
    
}

/** 加载更多 */
- (void)loadMoreData{
    static int currentCouunt = 1;

    NSMutableArray *subItems = [NSMutableArray arrayWithCapacity:12];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%d",currentCouunt * 12];
    [[NetWorkHelper shareInstance] GET:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  responseObject) {
        NSArray *items = responseObject[@"data"][@"hot_spot_list"];
        [items enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            EntityModel *model = [EntityModel entityModelWithDic:obj];
            model.nickImgUrl = obj[@"user"][@"avatar_s"];
            model.nickName = obj[@"user"][@"name"];
            [subItems addObject:model];
        }];
        
        currentCouunt++;
        [self.itemsArr addObjectsFromArray:subItems];
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/** 加载数据 */
- (void)loadData {
    kWeakSelf(weakSelf);
    [[NetWorkHelper shareInstance] GET:kMoreStoryUrl parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * responseObject) {
        
        NSArray *items = responseObject[@"data"][@"hot_spot_list"];
        [items enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            EntityModel *model = [EntityModel entityModelWithDic:obj];
            model.nickImgUrl = obj[@"user"][@"avatar_s"];
            model.nickName = obj[@"user"][@"name"];
            [weakSelf.itemsArr addObject:model];
        }];
        [weakSelf.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.itemsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.model = self.itemsArr[indexPath.item];
    
    return cell;
}






@end
